package com.otitan.forest.right.controller;

import com.otitan.forest.right.util.CommonUtil;
import com.otitan.forest.right.util.FileUtil;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 附件上传API
 * @Author: jiachangsen
 * @CreateDate: 2020/08/11 10:08
 * @UpdateUser: jiacs
 * @UpdateDate: 2020/08/11 10:08
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "附件上传API")
@RestController
@RequestMapping("/file")
public class FileController {

    @Value("${forest-right.file.shp-folder}")
    private String filePath;

    @RequiresRoles(value = {"admin", "ordinary"}, logical = Logical.OR)
    @ApiOperation(value = "单条记录导入GIS")
    @PostMapping("/upload")
    public DataResult upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return DataResult.fail("上传失败，请选择文件");
        }
        // 保存文件
        String fileNewPath = CommonUtil.uploadFile(filePath, file);

        Map<String, String> resMap = new HashMap<>();
        resMap.put("filePath", fileNewPath);
        return DataResult.success(resMap);
    }

    @RequiresRoles(value = {"admin", "ordinary"}, logical = Logical.OR)
    @ApiOperation(value = "多文件上传")
    @PostMapping("/batchUpload")
    public DataResult batchUpload(@RequestParam("files")MultipartFile[] files) {
        // 判断enctype属性是否为multipart/form-data

        if (files == null || files.length == 0) {
            return DataResult.fail("上传文件不能为空") ;
        }

        String fileNewPath = CommonUtil.batchUploadFile(files, filePath);

        Map<String, String> resMap = new HashMap<>();
        resMap.put("filePath", fileNewPath);
        return DataResult.success(resMap);
    }

    /**
     * @Description: 文件下载
     * @Author: xiazw
     * @CreateDate: 2020/6/15 15:39
     */
    @ApiOperation(value = "附件（文件）下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "filePath", required = true, value = "文件路径（需将路径转译后传入）")
    })
    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @PostMapping("download")
    public void download(HttpServletResponse response, @RequestBody String filePath) {
        FileUtil.downloadFile(response, filePath);
    }

    /**
     * @Description: 导入模板下载
     * @Author: wangqingsong
     * @CreateDate: 2020/8/13
     */
    @RequiresRoles(value = {"admin", "ordinary", "audit"}, logical = Logical.OR)
    @ApiOperation(value = "导入模板下载")
    @GetMapping("/importFile")
    public void importFile(HttpServletResponse response) {
        FileUtil.importFile(response);
    }
}
